package com.example.kaoqinmanager.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import com.example.kaoqinmanager.base.BaseBindingActivity;
import com.example.kaoqinmanager.bean.UserTable;
import com.example.kaoqinmanager.databinding.ActivityRegisterBinding;
import com.example.kaoqinmanager.utils.MyToast;

import org.litepal.LitePal;

import java.util.List;

public class RegisterActivity extends BaseBindingActivity<ActivityRegisterBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        actionBar.setTitle("注册");
        //显示返回箭头默认是不显示的
        actionBar.setDisplayHomeAsUpEnabled(true);

        getBinding().btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String strAccount = getBinding().edAccount.getText().toString().trim();
                String strPassWord = getBinding().edPassWord.getText().toString().trim();
                if (TextUtils.isEmpty(strAccount)) {
                    MyToast.showToast("账号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(strPassWord)) {
                    MyToast.showToast("密码不能为空");
                    return;
                }

                List<UserTable> userTables = LitePal.where("userName = ?", strAccount).find(UserTable.class);
                if (userTables.size() > 0) {
                    MyToast.showToast("该用户名已经被注册！");
                } else {
                    UserTable userTable = new UserTable();
                    userTable.setUserName(strAccount);
                    userTable.setPassWord(strPassWord);
                    userTable.save();

                    MyToast.showToast("注册成功");
                    finish();
                }


            }
        });


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //actionbar的左侧图标的点击事件处理
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}